#!/usr/bin/env bash
set -e

if [ -z "$1" ]
then
    echo 'Error: server url required.'
    exit 1
fi

if [ -z "$2" ]
then
    echo 'Error: docker-mirror url required.'
    exit 1
fi




SERVER_URL=$1
MIRROR_URL=$2
V_DOCKER_COMPOSE="1.24.1"
V_GIT="2.22.0"
V_MAVEN="3.6.1"

#################### 安装docker
curl -sSL https://get.daocloud.io/docker | sh && \
# 安装 Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/${V_DOCKER_COMPOSE}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
# 启动 docker
systemctl start docker && \
# 配置 Docker 加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s ${MIRROR_URL} && \
# 执行完毕后重启服务
systemctl restart docker && \
# 设置docker为自动启动
systemctl enable docker.service && \

###################  jenkins用到的相关工具安装
###### git安装
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc-c++ perl-ExtUtils-MakeMaker wget autoconf -y
rm -f git-${V_GIT}.tar.xz && \
wget -O git-${V_GIT}.tar.xz http://${SERVER_URL}/git-${V_GIT}.tar.xz && \
tar xvJf git-${V_GIT}.tar.xz && \
cd git-${V_GIT}
make configure
./configure --prefix=/usr/local/git
make profix=/usr/local/git
make install
echo "export PATH=$PATH:/usr/local/git/bin" > /etc/profile.d/git.sh && \
source /etc/profile.d/git.sh
##### maven安装
cd /usr/local/
rm -f apache-maven-${V_MAVEN}-bin.tar.gz && \
wget http://mirror.bit.edu.cn/apache/maven/maven-3/${V_MAVEN}/binaries/apache-maven-${V_MAVEN}-bin.tar.gz
tar -zxf apache-maven-${V_MAVEN}-bin.tar.gz && \
echo "export PATH=$PATH:/usr/local/apache-maven-${V_MAVEN}/bin" > /etc/profile.d/maven.sh && \
source /etc/profile.d/maven.sh && \
##### jdk安装
cd /usr/local/
rm -f jdk-8u211-linux-x64.tar.gz && \
wget http://${SERVER_URL}/jdk-8u211-linux-x64.tar.gz
tar -zxf jdk-8u211-linux-x64.tar.gz -C /usr/local && \
ln -sv /usr/local/jdk1.8.0_211 /usr/local/jdk1.8 && \
echo "export PATH=$PATH:/usr/local/jdk1.8/bin" >/etc/profile.d/java.sh && \
source /etc/profile.d/java.sh

################### docker形式安装jenkins
# 下载docker的jinkens镜像
docker pull jenkins/jenkins && \
# 创建jinkens映射目录
mkdir /jenkins/docker_jenkins_home && \
chmod 777 /jenkins/docker_jenkins_home && \
chmod 777 /usr/local/jdk1.8.0_211 && \
# Jenkins访问的端口号是8080，另外还需要暴露一个tcp的端口号50000。我们使用如下命令启动Jenkins镜像
docker run -d -p 80:8080 -p 50000:50000 \
-v /jenkins/docker_jenkins_home:/var/jenkins_home \
-v /etc/localtime:/etc/localtime \
-v /usr/local/jdk1.8.0_211:/usr/local/jdk \
-v /usr/local/git:/usr/local/git \
-v /usr/local/apache-maven-${V_MAVEN}:/usr/local/maven \
-e JAVA_OPTS=-Duser.timezone=Asia/Shanghai \
--restart=on-failure:10 \
--name jenkins jenkins/jenkins
